Conditions | 20 |
Paths | 74 |
Total Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like formManager.getFormFields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | var formManager = function () { |
||
110 | getFormFields: function (form, isSubmission) { |
||
111 | var usernameField = null; |
||
112 | |||
113 | // Locate the password field(s) in the form. Up to 3 supported. |
||
114 | // If there's no password field, there's nothing for us to do. |
||
115 | var pwFields = this._getPasswordFields(form, isSubmission); |
||
116 | if (!pwFields) { |
||
117 | usernameField = this._getUsernameFields(form); |
||
118 | if(usernameField && usernameField.hasOwnProperty('element')) { |
||
119 | return [usernameField.element, null, null]; |
||
120 | } else { |
||
121 | return [null, null, null]; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | |||
126 | // Locate the username field in the form by searching backwards |
||
127 | // from the first passwordfield, assume the first text field is the |
||
128 | // username. We might not find a username field if the user is |
||
129 | // already logged in to the site. |
||
130 | for (var i = pwFields[0].index - 1; i >= 0; i--) { |
||
131 | if (!this.isElementVisible(form.elements[i])) { |
||
132 | continue; |
||
133 | } |
||
134 | if (form.elements[i].type.toLowerCase() === "text" || form.elements[i].type.toLowerCase() === "email") { |
||
135 | usernameField = form.elements[i]; |
||
136 | break; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if (!usernameField) { |
||
141 | this.log('(form (' + form.action + ') ignored -- no username field found)'); |
||
142 | } |
||
143 | |||
144 | |||
145 | // If we're not submitting a form (it's a page load), there are no |
||
146 | // password field values for us to use for identifying fields. So, |
||
147 | // just assume the first password field is the one to be filled in. |
||
148 | if (!isSubmission || pwFields.length === 1) { |
||
149 | var res = [usernameField, pwFields[0].element]; |
||
150 | if (pwFields[1]) { |
||
151 | res.push(pwFields[1].element); |
||
152 | } else { |
||
153 | res.push(null); |
||
154 | } |
||
155 | return res; |
||
156 | } |
||
157 | |||
158 | |||
159 | // Try to figure out WTF is in the form based on the password values. |
||
160 | var oldPasswordField, newPasswordField; |
||
161 | var pw1 = pwFields[0].element.value; |
||
162 | var pw2 = pwFields[1].element.value; |
||
163 | var pw3 = (pwFields[2] ? pwFields[2].element.value : null); |
||
164 | |||
165 | if (pwFields.length === 3) { |
||
166 | // Look for two identical passwords, that's the new password |
||
167 | |||
168 | if (pw1 === pw2 && pw2 === pw3) { |
||
169 | // All 3 passwords the same? Weird! Treat as if 1 pw field. |
||
170 | newPasswordField = pwFields[0].element; |
||
171 | oldPasswordField = null; |
||
172 | } else if (pw1 === pw2) { |
||
173 | newPasswordField = pwFields[0].element; |
||
174 | oldPasswordField = pwFields[2].element; |
||
175 | } else if (pw2 === pw3) { |
||
176 | oldPasswordField = pwFields[0].element; |
||
177 | newPasswordField = pwFields[2].element; |
||
178 | } else if (pw1 === pw3) { |
||
179 | // A bit odd, but could make sense with the right page layout. |
||
180 | newPasswordField = pwFields[0].element; |
||
181 | oldPasswordField = pwFields[1].element; |
||
182 | } else { |
||
183 | // We can't tell which of the 3 passwords should be saved. |
||
184 | this.log("(form ignored -- all 3 pw fields differ)"); |
||
185 | return [null, null, null]; |
||
186 | } |
||
187 | } else { // pwFields.length == 2 |
||
188 | if (pw1 === pw2) { |
||
189 | // Treat as if 1 pw field |
||
190 | newPasswordField = pwFields[0].element; |
||
191 | oldPasswordField = null; |
||
192 | } else { |
||
193 | // Just assume that the 2nd password is the new password |
||
194 | oldPasswordField = pwFields[0].element; |
||
195 | newPasswordField = pwFields[1].element; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return [usernameField, newPasswordField, oldPasswordField]; |
||
200 | }, |
||
201 | log: function (str) { |
||
284 | formManager._init_(); |